package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.widget.LoadingDialog;

import java.util.List;

/**
 * Created by Administrator on 2016/8/5.
 */
public class VipSeeCarActivity extends BaseActivity implements AMap.InfoWindowAdapter {

    private String address = "";

    private LoadingDialog loadingDialog;

    private MapView mMapView;

    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vip_map_layout);
        setTitle("看车地址");
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState); // 此方法必须重写
        aMap = mMapView.getMap();
        aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
        address = getIntent().getStringExtra("address");
        loadingDialog = new LoadingDialog( this , "定位中");

        GeocodeSearch geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                loadingDialog.dismiss();
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
                loadingDialog.dismiss();
                try {
                    List<GeocodeAddress> geocodeAddresses = geocodeResult.getGeocodeAddressList();
                    if(geocodeAddresses.size() > 0) {
                        MarkerOptions markerOption = new MarkerOptions();
                        markerOption.draggable(false);
                        markerOption.anchor(0.5f, 0.5f);
                        markerOption.title("成都市").snippet(address);
                        LatLonPoint latLonPoint = geocodeAddresses.get(0).getLatLonPoint();
                        LatLng latLng = new LatLng(latLonPoint.getLatitude() , latLonPoint.getLongitude());
                        markerOption.position(latLng);
                        markerOption.icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.icon_map_location));
                        Marker marker = aMap.addMarker(markerOption);
                        aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
                        marker.showInfoWindow();
                    }
                } catch (Exception e) {
                    Toast.makeText(VipSeeCarActivity.this, "获取位置异常, 请稍后再试", Toast.LENGTH_SHORT).show();
                }
            }
        });
        GeocodeQuery query = new GeocodeQuery(address, "成都");
        geocodeSearch.getFromLocationNameAsyn(query);
        loadingDialog.show();
    }


    /**
     * 方法必须重写
     */
    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = getLayoutInflater().inflate(
                R.layout.custom_info_window, null);

        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定义infowinfow窗口
     */
    public void render(Marker marker, View view) {

        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.title));
        titleUi.setText(title);

        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        snippetUi.setText(snippet);

    }

    @Override
    public View getInfoContents(Marker marker) {
      return  null;
    }
}
